<template>
  <div class="login-container">
    <div class="login-box">
      <div class="login-header">
        <h2>学生管理系统</h2>
        <p>欢迎登录</p>
      </div>
      <el-form
        ref="loginFormRef"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        label-width="80px"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            v-model="loginForm.username"
            placeholder="请输入用户名"
            prefix-icon="User"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="loginForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="Lock"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            class="login-btn"
            @click="handleLogin"
            :loading="loading"
            :disabled="loading"
          >
            登录
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const router = useRouter()
const loginFormRef = ref(null)
const loading = ref(false)
const loginForm = reactive({
  username: '',
  password: ''
})

const loginRules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 20, message: '用户名长度在 3 到 20 个字符', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度在 6 到 20 个字符', trigger: 'blur' }
  ]
}

const handleLogin = async () => {
  // 验证表单
  try {
    await loginFormRef.value.validate()
    loading.value = true
    
    // 模拟登录请求
    // 实际项目中这里应该调用真实的登录API
    await new Promise(resolve => setTimeout(resolve, 1000))
    
    // 模拟登录成功，保存token和用户信息
    const token = 'mock-jwt-token-' + Date.now()
    const userInfo = {
      id: 1,
      username: loginForm.username,
      role: 'admin'
    }
    
    localStorage.setItem('token', token)
    localStorage.setItem('userInfo', JSON.stringify(userInfo))
    
    // 跳转到首页或之前的页面
    const redirect = router.currentRoute.value.query.redirect || '/'    
    router.push(redirect)
  } catch (error) {
    console.error('登录失败:', error)
    if (error.name === 'Error') {
      // 显示错误信息
      ElMessage.error('用户名或密码错误，请重试')
    }
  } finally {
    loading.value = false
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
}

.login-box {
  width: 400px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.15);
  padding: 32px;
}

.login-header {
  text-align: center;
  margin-bottom: 24px;
}

.login-header h2 {
  margin: 0 0 8px 0;
  font-size: 24px;
  font-weight: bold;
  color: #1890ff;
}

.login-header p {
  margin: 0;
  color: #666;
}

.login-form {
  width: 100%;
}

.login-btn {
  width: 100%;
  font-size: 16px;
  padding: 10px;
}

@media (max-width: 576px) {
  .login-box {
    width: 90%;
    padding: 24px;
  }
}
</style>